package ro.flashbite.loggers.targets
{
	import ro.flashbite.errors.ArgumentIncompatibleError;
	import ro.flashbite.interfaces.IOutputable;
	import ro.flashbite.loggers.LoggerLevels;

	/**
	 * This class creates a smart trace representation of an object and pass the string to a IOutputable class to render it
	 * 
	 * v 1.0
	 * 
	 * @author Adrian Barbu
	 */	
	public final class ConsoleLoggingTarget extends TraceLoggingTarget
	{
		/**
		 * class that can 'render' text
		 */		
		private var _output:IOutputable;
		/**
		 * direct acces to transformation to color function for code performance 
		 */		
		private var _levelToColorFunc:Function = LoggerLevels.levelToColor;
		
		// ================================================================================================================================
		// CONSTRUCTOR
		// ================================================================================================================================
		
		/**
		 * @param output: (IOutputable) a class that will be used to output messages
		 */		
		public function ConsoleLoggingTarget(output:IOutputable) {
			_constructor(output);
		}
		private function _constructor(output:IOutputable):void
		{
			_output = output;
			if (_output == null) throw new ArgumentIncompatibleError("output");
		}
		
		// ================================================================================================================================
		// ILogTarget implementation
		// ================================================================================================================================
		
		/**
		 * Create a smart trace output based on logger and level, and pass it to the output class
		 */		
		override public function logMessage(loggerName:String, level:int, ...message):void
		{
			_output.appendText(super.createMessage(loggerName, level, message), _levelToColorFunc(level));
		}
		
		// ================================================================================================================================
		// DESTRUCTOR
		// ================================================================================================================================
		
		/** 
		 * Proper destroy
		 */		
		override public function destroy():void
		{
			_output = null;
			_levelToColorFunc = null;
			
			super.destroy();
		}
	}
}